﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Ds.DataBase;

namespace Domain.RealiZation.B
{
    /// <summary>
    /// 1.基础操作 基础类型操作
    /// 重点实现基础接口中的操作
    /// </summary>
    internal class BaseOperate<T> where T : class
    {
        public UserInfoClassContext Context = null;
        public BaseOperate()
        {
            Context = new UserInfoClassContext();
        }

        #region 其他操作 
        /// <summary>  
        /// 保存数据  
        /// </summary>  
        /// <returns></returns>  
        public int Save()
        {
            return Context.SaveChanges();
        }
        /// <summary>  
        /// 标间对象为修改状态，并保存  
        /// </summary>  
        public bool SetModify<T>(T model) where T : class
        {
            Context.Entry<T>(model).State = EntityState.Modified;
            return Save() > 0;
        }
        #endregion

        #region 基础访问
        /// <summary>
        /// 获取表达树
        /// </summary>
        /// <returns></returns>
        public DbSet<T> GetDbSet()
        {
            return Context.Set<T>();
        }
        /// <summary>
        /// 根据主键 获取对象
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public T GetModel(object id)
        {
            return GetDbSet().Find(id);
        }
        /// <summary>
        /// 添加对象
        /// </summary>
        /// <param name="model">实例对象</param>
        /// <returns></returns>
        public bool Add(T model)
        {
            try
            {
                GetDbSet().Add(model);
                return Save() > 0;
            }
            catch (Exception ex)
            {
                return Error_Add(ex);
            }
        }
        /// <summary>
        /// 修改对象
        /// </summary>
        /// <param name="model">实例对象</param>
        /// <returns></returns>
        public bool Modify(T model)
        {
            try
            {
                return SetModify(model);
            }
            catch (Exception ex)
            {
                return Error_Modify(ex);
            }
        }
        /// <summary>
        /// 删除对象
        /// </summary>
        /// <param name="model">实例对象</param>
        /// <returns></returns>
        public bool Delete(T model)
        {
            try
            {
                GetDbSet().Remove(model);
                return Save() > 0;
            }
            catch (Exception ex)
            {
                return Error_Delete(ex);
            }
        }
        /// <summary>
        /// 删除 对象
        /// </summary>
        /// <param name="id">主键ID</param>
        /// <returns></returns>
        public bool Delete(object id)
        {
            try
            {
                T model = GetModel(id);
                if (model == null)
                    throw new Exception($"获取id={id}的对象失败,当前类型:{model.GetType().Name}");
                return Delete(model);
            }
            catch (Exception ex)
            {
                return Error_Delete(ex);
            }
        }
        #endregion

        #region 异常处理  
        private string GetErrorMsg(Exception ex)
        {
            StringBuilder builder = new StringBuilder();
            Exception inner = ex;
            int i = 0;
            while (inner != null)
            {
                i++;
                builder.AppendFormat("内部原因{0}:{1}", i, ex.Message);
                builder.AppendLine();
                inner = inner.InnerException;
            }
            return builder.ToString();
        }
        /// <summary>  
        /// 添加数据异常  
        /// </summary>  
        /// <param name="ex"></param>  
        /// <returns></returns>  
        public bool Error_Add(Exception ex)
        {
            throw new Exception("添加数据失败，" + GetErrorMsg(ex));
        }
        /// <summary>  
        /// 修改数据异常  
        /// </summary>  
        /// <returns></returns>  
        public bool Error_Modify(Exception ex)
        {
            throw new Exception("修改数据失败，" + GetErrorMsg(ex));
        }
        /// <summary>  
        /// 删除数据异常  
        /// </summary>  
        public bool Error_Delete(Exception ex)
        {
            throw new Exception("删除数据失败，" + GetErrorMsg(ex));
        }
        #endregion
    }
}
